<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">

        <a-row>
          <a-col :span="24">
            <a-form-model-item label="父级部门" :labelCol="labelColOne" :wrapperCol="wrapperColOne" prop="parentId">
              <a-tree-select
                style="width:100%"
                :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }"
                :treeData="deptTreeData"
                v-model="model.parentId"
                placeholder="请选择父级部门"
              >
              </a-tree-select>
            </a-form-model-item>
          </a-col>
        </a-row>


        <a-row>
          <a-col :span="12">
            <a-form-model-item label="部门名称" :labelCol="labelColTwoFirst" :wrapperCol="wrapperColTwoFirst" prop="name">
              <a-input v-model="model.name" placeholder="请输入部门名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="序号" :labelCol="labelColTwo" :wrapperCol="wrapperColTwo" prop="orderNo">
              <a-input v-model="model.orderNo" placeholder="请输入序号"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="24">
            <a-form-model-item label="描述" :labelCol="labelColOne" :wrapperCol="wrapperColOne" prop="description">
              <a-input v-model="model.description" placeholder="请输入描述"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import ATextarea from 'ant-design-vue/es/input/TextArea'
  import { requestSetData } from '../../../utils/commonUtils'
  import { FormMixin } from '../../../mixins/FormMixin'

  export default {
    name: 'DeptForm',
    mixins: [ FormMixin],
    components: {
      ATextarea
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        deptTreeData:[],
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/user/dept/save",
          edit: "/user/dept/update",
          queryById: "/user/dept/detail"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));


      let $this = this;
      requestSetData("/user/dept/listTree",{},function(data){
        $this.deptTreeData = data;
        $this.deptTreeData.splice(0,0, {title:'根目录',value:'0'});
        // $this.deptTreeData.forEach(data=>{
        //   $this.expandedKeys.push(data.key);
        // })
      });


    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        // this.model.dataSource = this.model.dataSource || {id:null};
        // this.model.template = this.model.template || {id:null};
        this.visible = true;
      },
      handleChange(value) {
        console.log(`selected ${value}`);
      },
      onChange(e) {
        console.log('radio1 checked', e.target.value);
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            if(!this.model.id){
              httpurl+=this.url.add;
            }else{
              httpurl+=this.url.edit;
            }
            httpAction(httpurl,this.model,"post").then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>